﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Lazytools : Editor
{

    
    [MenuItem("Tools/Lazytools/构建图片信息")]
    private static void CreateSpriteInfo()
    {
        IOUntil.GetObjectNameToArray("Resours/Sprites", ".png", true);
        string temp = null;

        string[] temps = new string[11];
        for (int i = 0; i < IOUntil.nameArray.Count; i++)
        {
           
            temps = IOUntil.nameArray[i].Split('/');
            //Debug.LogWarning(IOUntil.nameArray[i]);
            if (/*temps[temps.Length-1].Contains("Templar")&& */ temps[9].Contains("Firearm1H"))
            {
                temp += IOUntil.nameArray[i].Replace(".png", "").Replace("D:/MyProject/GitProject/Dreamofdust-round/Assets/HeroEditor4D/FantasyHeroes/Sprites/", "") + "\r\n";
                //temp += /*temps[9]+"-"+ temps[10]+ "-"+ */IOUntil.nameArray[i].Replace(".png","")/*+"-"+ temps[4]*/ + "\r\n";
                //Until.WriteTypeAndPath(IOUntil.nameArray[i], "Sprite_" + temps[2] + temps[1] + "_" + (temps[3].Contains("Body") ? "Body_" : "Equip_") + temps[4] + "_" + temps[5].Replace(" [Preview]", "") + "_");
            }

        }
        Debug.LogWarning(temp);
    }

    [MenuItem("Tools/Lazytools/构建天赋信息")]
    private static void CreatePassivesInfo()
    {
        //IOUntil.GetObjectNameToArray("Scripts/BattleSystem/Status/Passives", ".png", true);
        //string temp = null;
        //string[] temps = new string[10];
        //string[] tempss = new string[2];
        //for (int i = 0; i < IOUntil.nameArray.Count; i++)
        //{

        //    temps = IOUntil.nameArray[i].Split('/');
        //    tempss = temps[9].Split('.');
        //    var config = Resources.Load<StatusConfigObject>("StatusConfigs/Passives/" + tempss[0].Replace("Status", "Status_") + "00");
        //    //config = config==null? Resources.Load<StatusConfigObject>("StatusConfigs/Passives/" + tempss[0].Replace("Status", "Status_")):config;
        //    string configstr = config == null ? "configs" : "Resources.Load<StatusConfigObject>(\"StatusConfigs/Passives/" + tempss[0].Replace("Status", "Status_") + "\"" + "+temps[1])";
        //    temp += "case    \"" + tempss[0] + "\":\r\n   passive = AttachStatus<" + tempss[0] + ">(" + configstr + ");\r\n break;\r\n";
        //    //temp += tempss[0] + "\r\n ";
        //    //if (temps[3].Contains("Body")/*&& temps[4].Contains(EquipType.Bow.ToString())*/)
        //    //{
        //    //    temp += IOUntil.nameArray[i] + "-" + temps[4] + "\r\n";
        //    //    //Until.WriteTypeAndPath(IOUntil.nameArray[i], "Sprite_" + temps[2] + temps[1] + "_" + (temps[3].Contains("Body") ? "Body_" : "Equip_") + temps[4] + "_" + temps[5].Replace(" [Preview]", "") + "_");
        //    //}

        //}
        //Debug.LogWarning(temp);
    }


    [MenuItem("Tools/Lazytools/移除丢失脚本")]
    static void CleanupMissingScripts()
    {

        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            GameObjectUtility.RemoveMonoBehavioursWithMissingScript(Selection.gameObjects[i]);
            var gameObject = Selection.gameObjects[i];

            // We must use the GetComponents array to actually detect missing components
            var components = gameObject.GetComponents<Component>();

            // Create a serialized object so that we can edit the component list
            var serializedObject = new SerializedObject(gameObject);
            // Find the component list property
            var prop = serializedObject.FindProperty("m_Component");

            // Track how many components we've removed
            int r = 0;

            // Iterate over all components
            for (int j = 0; j < components.Length; j++)
            {
                // Check if the ref is null
                if (components[j] == null)
                {
                    // If so, remove from the serialized component array
                    prop.DeleteArrayElementAtIndex(j - r);
                    // Increment removed count
                    r++;
                }
            }
            // Apply our changes to the game object
            serializedObject.ApplyModifiedProperties();
            EditorUtility.SetDirty(gameObject);
        }
    }

    /// <summary>
    /// 将构建场景设置为默认。
    /// </summary>
    [MenuItem("Game Framework/Scenes in Build Settings/Current Scenes", false, 23)]
    public static void DefaultScenes()
    {
        HashSet<string> sceneNames = new HashSet<string>();
        var s_DefaultSceneNames= IOUntil.GetObjectNameToArray("GameMain/Scenes", "unity");
        foreach (string sceneName in s_DefaultSceneNames)
        {
            //Debug.LogWarning(sceneName);
            sceneNames.Add("Assets/GameMain/Scenes/" + sceneName + ".unity");

        }
       
        List<EditorBuildSettingsScene> scenes = new List<EditorBuildSettingsScene>();
        foreach (string sceneName in sceneNames)
        {
            if(sceneName.Contains("Logo"))
            {
                scenes.Insert(0,new EditorBuildSettingsScene(sceneName, true));
            }
            else
            {
                scenes.Add(new EditorBuildSettingsScene(sceneName, true));
            }
           
        }

        EditorBuildSettings.scenes = scenes.ToArray();

        Debug.Log("Set scenes of build settings to current scenes.");
    }
}
